//
//  CNCarView.m
//  BeeQuick_19
//
//  Created by 张旭峰 on 16/9/23.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNCarView.h"
@interface CNCarView ()
@property (weak, nonatomic) IBOutlet UIButton *decreaseButton;
@property (weak, nonatomic) IBOutlet UILabel *numLabel;
@property (weak, nonatomic) IBOutlet UIButton *increaseLabel;
@property (weak, nonatomic) IBOutlet UIButton *countBtn;

@end
@implementation CNCarView
- (void)awakeFromNib{
    
    self.isIncrease = NO;
    
}
-(void)setModel:(CNdetailsModel *)model
{
    _model = model;
}
- (void)setCount:(NSInteger)count{
    
    _count = count;
    
    //    NSLog(@"Car里的count---------->%zd",count);
    [self.countBtn setTitle:[NSString stringWithFormat:@"%ld",(long)self.count] forState:UIControlStateNormal];
    // 判断数量,控制控件是否隐藏
    if (count > 0) {
        // 数量有值,可以进行减少的功能!
        _decreaseButton.hidden = NO;
        _numLabel.hidden = NO;
    } else {
        
        // 数量为0,隐藏控件!
        _decreaseButton.hidden = YES;
        _numLabel.hidden = YES;
    }
    
    // 将数量信息显示到label上!
    _numLabel.text = @(count).description;
    
}
+ (instancetype)bottomCarView{
    return [[NSBundle mainBundle] loadNibNamed:@"CNCarView" owner:nil options:nil] .lastObject;
}

- (IBAction)clickDecreaseBtn:(UIButton *)sender {
    
    if (self.model.count > 0) {
        
        self.count = self.model.count;
    }
    
    self.isIncrease = NO;
    self.count--;
    self.model.count = self.count;
    
    //代理传值
    if ([self.delegate respondsToSelector:@selector(decreaseWithModel:)]) {
        
        [self.delegate decreaseWithModel:self.model];
    }
    [[SQLiteManager sharedSQLiteManager]charu:_model With:-1];

    [self.countBtn setTitle:[NSString stringWithFormat:@"%ld",(long)self.count] forState:UIControlStateNormal];

}

- (IBAction)clickIncreaseBtn:(UIButton *)sender {
    
    if (self.model.count > 0) {
        
        self.count = self.model.count;
    }
    
    self.isIncrease = YES;
    
    self.count++;
    
    self.model.count = self.count;
    
    //代理传值
    if ([self.delegate respondsToSelector:@selector(increaseWithModel:)]) {
        
        [self.delegate increaseWithModel:self.model];
    }
    [[SQLiteManager sharedSQLiteManager]charu:_model With:1];

    [self.countBtn setTitle:[NSString stringWithFormat:@"%ld",(long)self.count] forState:UIControlStateNormal];
    
    //    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"reddot"]];
    UIImageView *imgView =[[UIImageView alloc] initWithImage:[[SDImageCache sharedImageCache] imageFromDiskCacheForKey:_model.img]];
    imgView.center = CGPointMake(150, 647);
    imgView.bounds = CGRectMake(0, 0, 50, 50);
    // - 将图片框添加给窗口
    UIWindow *keyW = [UIApplication sharedApplication].keyWindow;
    [keyW addSubview:imgView];
    // 1.创建对象
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    // 只有kvc可以这么做,其他的对象,不ok!
    [anim setValue:imgView forKey:@"imgV"];
    // - 路径对象
    UIBezierPath *path = [UIBezierPath bezierPath];
    // - 移动到起点
    [path moveToPoint:CGPointMake(150, 647)];
    // 结束点
    CGPoint endP = CGPointMake(keyW.bounds.size.width*0.90, keyW.bounds.size.height*0.88);
    // 控制点
    CGPoint controlP = CGPointMake(280, 570);
    // 添加完美路径
    [path addQuadCurveToPoint:endP controlPoint:controlP];
    
    anim.path = path.CGPath;
    
    // 不让闪回去
    anim.removedOnCompletion = NO;
    anim.fillMode = kCAFillModeForwards;
    
    anim.duration = 0.5;
    
    // 设置动画的代理
    anim.delegate = self;
    
    // 3.添加
    [imgView.layer addAnimation:anim forKey:nil];
    
    [UIView animateWithDuration:1 animations:^{
        
        imgView.transform = CGAffineTransformScale(imgView.transform, 0.3, 0.3);
        
        imgView.alpha = 0.4;
    }];

    
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    
    // 1.移除图片框
    // - 取出图片框
    UIImageView *imgV = [anim valueForKey:@"imgV"];
    // - 移除核心动画
    [imgV.layer removeAllAnimations];
    
    // - 移除
    [imgV removeFromSuperview];
    
    
}
@end
